package com.jprocms.module.cms.api;

import com.jpro.framework.common.pojo.CommonResult;
import com.jpro.module.system.api.permission.PermissionApi;
import com.jpro.module.system.api.permission.dto.ChannelPermissionRespDTO;
import com.jpro.module.system.api.permission.dto.ContentPermissionRespDTO;
import com.jpro.module.system.api.permission.dto.DeptDataPermissionRespDTO;
import com.jpro.module.system.api.permission.dto.SitePermissionRespDTO;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.Set;

import static com.jpro.framework.common.pojo.CommonResult.error;
import static com.jpro.framework.common.pojo.CommonResult.success;
import static com.jprocms.module.cms.enums.ErrorCodeConstants.COMMON_RPC_SYSTEM_ERROR;

@RestController // 提供 RESTful API 接口，给 Feign 调用

@Validated
public class PermissionApiImpl implements PermissionApi {

    @Resource
    private PermissionApi permissionService;

    @Override
    public CommonResult<Set<Long>> getUserRoleIdListByRoleIds(Collection<Long> roleIds) {
        CommonResult<Set<Long>> result = permissionService.getUserRoleIdListByRoleIds(roleIds);
        if (result.isSuccess()) {
            return success(result.getData());
        }
        return error(COMMON_RPC_SYSTEM_ERROR);
    }

    @Override
    public CommonResult<Boolean> hasAnyPermissions(Long userId, String... permissions) {
        CommonResult<Boolean> result = permissionService.hasAnyPermissions(userId, permissions);
        if (result.isSuccess()) {
            return success(result.getData());
        }
        return error(COMMON_RPC_SYSTEM_ERROR);
    }

    @Override
    public CommonResult<Boolean> hasAnyRoles(Long userId, String... roles) {
        CommonResult<Boolean> result = permissionService.hasAnyRoles(userId, roles);
        if (result.isSuccess()) {
            return success(result.getData());
        }
        return error(COMMON_RPC_SYSTEM_ERROR);
    }

    @Override
    public CommonResult<DeptDataPermissionRespDTO> getDeptDataPermission(Long userId) {
        CommonResult<DeptDataPermissionRespDTO> result = permissionService.getDeptDataPermission(userId);
        if (result.isSuccess()) {
            return success(result.getData());
        }
        return error(COMMON_RPC_SYSTEM_ERROR);
    }

    @Override
    public CommonResult<ChannelPermissionRespDTO> getChannelPermission(Long userId) {
        CommonResult<ChannelPermissionRespDTO> result = permissionService.getChannelPermission(userId);
        if (result.isSuccess()) {
            return success(result.getData());
        }
        return error(COMMON_RPC_SYSTEM_ERROR);
    }

    @Override
    public CommonResult<ContentPermissionRespDTO> getContentPermission(Long userId) {
        CommonResult<ContentPermissionRespDTO> result = permissionService.getContentPermission(userId);
        if (result.isSuccess()) {
            return success(result.getData());
        }
        return error(COMMON_RPC_SYSTEM_ERROR);
    }

    @Override
    public CommonResult<SitePermissionRespDTO> getSitePermission(Long userId) {
        CommonResult<SitePermissionRespDTO> result = permissionService.getSitePermission(userId);
        if (result.isSuccess()) {
            return success(result.getData());
        }
        return error(COMMON_RPC_SYSTEM_ERROR);
    }

    @Override
    public CommonResult<Boolean> hasChannelPermission(Long userId, Long channelId) {
        CommonResult<Boolean> result = permissionService.hasChannelPermission(userId, channelId);
        if (result.isSuccess()) {
            return success(result.getData());
        }
        return error(COMMON_RPC_SYSTEM_ERROR);
    }
}
